<template>
  <!-- 流程定义 -->
  <div>
    <el-row>
      <el-col style="margin-right: 10px" class="content-left">
        <el-row class="handleBox">
          <el-col :span="18">
            <template>
              <el-button type="normal" @click="process">流程进度</el-button>
            </template>
          </el-col>
          <el-col :span="6">
            <div class="handleIconBox">
              <el-popover
                placement="bottom"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="search_porper">
                  <el-form
                    ref="searchForm"
                    :model="searchForm"
                    label-width="90px"
                    label-position="left"
                    size="mini"
                  >
                    <el-form-item label="任务名称：">
                      <el-input
                        v-model="searchForm.taskDefName"
                        style="width: 180px"
                      />
                    </el-form-item>
                    <el-form-item label="流程名称：">
                      <el-input
                        v-model="searchForm.processDefName"
                        style="width: 180px"
                      />
                    </el-form-item>
                    <el-form-item label="发起人：">
                      <div class="treeBoxBig">
                        <div class="treeBox">
                          {{ searchForm.processInstanceUserName }}
                        </div>
                        <div class="treeBtnCS" @click="initiatorBtn" />
                      </div>
                    </el-form-item>
                  </el-form>
                  <div style="text-align: center; padding: 10px">
                    <el-button
                      size="mini"
                      @click="(searchForm.page = 1), searchFunc()"
                      >查询</el-button
                    >
                    <el-button size="mini" @click="searchClear">重置</el-button>
                  </div>
                </div>
                <i slot="reference" class="iconfont icon-sousuo" title="搜索" />
              </el-popover>
              <el-popover
                placement="bottom"
                width="150"
                trigger="click"
                popper-class="icon_porper"
              >
                <div class="operation_checkbox">
                  <el-checkbox-group v-model="checkList">
                    <el-checkbox
                      v-for="item in textData"
                      :key="item.prop"
                      :label="item.label"
                      >{{ item.label }}
                    </el-checkbox>
                  </el-checkbox-group>
                </div>
                <i
                  slot="reference"
                  class="iconfont icon-liebiaoshezhi"
                  title="列表设置"
                />
              </el-popover>
            </div>
          </el-col>
        </el-row>

        <el-table
          ref="multipleTable"
          :data="tableData"
          @row-click="authorizeFor"
        >
          <el-table-column width="55" type="selection"  />
          <el-table-column
            v-if="columnState[0]"
            prop="taskDefName"
            label="任务名称 "
            show-overflow-tooltip
          />

          <el-table-column
            v-if="columnState[1]"
            prop="processDefName"
            label="流程名称"
            show-overflow-tooltip
          />

          <el-table-column
            v-if="columnState[3]"
            prop="createdTm"
            label="接收时间"
            sortable
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <div v-if="scope.row.createdTm">
                {{ timeFormat(scope.row.createdTm) }}
              </div>
              <div v-else>-</div>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnState[4]"
            prop="processInstanceUserName"
            label="流程发起人"
            sortable
            :formatter="(row) => row.processInstanceUserName || '-'"
          />
        </el-table>
        <pagination
          :current-page="searchForm.page"
          :page-size="searchForm.size"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          @refresh="searchFunc"
        />
      </el-col>
    </el-row>
    <!-- 审批弹框 -->
    <el-dialog
      v-dialogDrag
      title="任务进度"
      :visible.sync="taskVisible"
      width="50%"
    >
      <commonTab
        ref="commonTab"
        :save-titles="saveTitles"
        :businessKey="businessKey"
        :processInstanceId="processInstanceId"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="taskVisible = false">关闭</el-button>
      </div>
    </el-dialog>
    <!-- 发起人弹窗 -->
    <el-dialog
      v-dialogDrag
      title="选择发起人"
      :visible.sync="reassignmentVisible"
      width="50%"
    >
      <reassignmentTable
        ref="reassignmentTable"
        @sPrincipalUser="sPrincipalUser"
        @reassignClose="reassignClose"
      />
    </el-dialog>
  </div>
</template>
<script>
import commonTab from "@/views/workFlow/components/commonTab";
import reassignmentTable from "@/views/workFlow/components/reassignmentTable";
import { workflow } from "@/api/index";
import moment from "moment";
export default {
  components: {
    reassignmentTable,
    commonTab,
  },
  data() {
    return {
      businessKey: "",
      processInstanceId: "",
      row: [],
      detailsForm: {},
      reassignmentVisible: false,
      saveTitles: {
        list: [
          {
            title: "任务审批",
            show: false,
          },
          {
            title: "审批历史",
            show: true,
          },
          {
            title: "流程进度图",
            show: true,
          },
          {
            title: "业务表单",
            show: false,
          },
        ],
        activeName: "second",
      },
      taskVisible: false,
      searchForm: {
        processDefCode: "",
        processDefName: "",
        taskDefName: "",
        processInstanceUserName: "",
        page: 1,
        size: 15,
      },
      total: 0,
      checkList: [],
      // 列隐藏
      textData: [
        { label: "任务名称" },
        { label: "流程名称" },
        { label: "接收时间" },
        { label: "流程发起人" },
        { label: "待审批人" },
      ],
      // 获取table的数据
      tableData: [],
    };
  },
  computed: {
    columnState() {
      const arr = [];
      const arr1 = this.textData;
      const arr2 = this.checkList;
      for (let i = 0; i < arr1.length; i++) {
        let flg = false;
        for (let j = 0; j < arr2.length; j++) {
          if (arr1[i].label === arr2[j]) {
            flg = true;
          }
        }
        arr.push(flg);
      }
      return arr;
    },
  },
  created() {
    this.checkListInit();
    this.searchFunc();
  },
  mounted() {},
  methods: {
    // 点击发起人
    initiatorBtn() {
      this.reassignmentVisible = true;
      setTimeout(() => {
        this.$refs.reassignmentTable.init({ id: "0" });
        this.$refs.reassignmentTable.getOrgList();
      }, 30);
    },
    reassignClose() {
      this.reassignmentVisible = false;
    },
    sPrincipalUser(val) {
      this.searchForm.processInstanceUserName = val.userName;
      this.reassignmentVisible = false;
    },
    searchClear() {
      for (const key in this.searchForm) {
        this.searchForm[key] = "";
      }
      this.searchForm.page = 1;
      this.searchForm.size = 15;
    },
    checkListInit() {
      this.checkList = this.textData.map((v) => {
        return v.label;
      });
    },
    searchFunc() {
      const data = this.searchForm;
      workflow.queryForPageTaskInstanceHistory(data).then((res) => {
        this.tableData = res.datas.rows;
        this.total = res.datas.total;
      });
    },
    // 流程进度
    process() {
      const selection = this.$refs.multipleTable.selection;
      if (selection.length !== 1) {
        return this.$message.warning("请选择一条数据");
      }
      this.businessKey = selection[0].businessKey;
      this.processInstanceId = selection[0].processInstanceId;
      this.row = selection;
      this.taskVisible = true;
      setTimeout(() => {
        this.$refs.commonTab.init();
      }, 30);
    },
    authorizeFor(row) {
      if (this.$refs.multipleTable) {
        this.$refs.multipleTable.clearSelection();
      }
      this.$refs.multipleTable.toggleRowSelection(row, true);
      this.userName = row.roleName;
    },
    timeFormat(val) {
      if (val) return moment(val).format("YYYY-MM-DD HH:mm:ss");
      else return "";
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.searchForm.size = val;
      this.searchFunc();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.searchForm.page = val;
      this.searchFunc();
    },
  },
};
</script>

<style lang="scss" scoped>
.treeBoxBig {
  width: 180px;
  height: 32px;
  border: 1px solid #dce0e6;
  border-radius: 4px;
  color: white;
  .treeBox {
    width: 140px;
    height: 32px;
    padding-left: 10px;
    line-height: 32px;
  }
  .treeBtnCS {
    background: url("~@/assets/images/popWindow_arrow.gif");
    background-repeat: no-repeat;
    height: 32px;
    width: 32px;
    position: absolute;
    left: 156px;
    top: 8px;
    cursor: pointer;
  }
}
</style>
